群組相關事件有4種
from linebot.models.events import (
JoinEvent,
LeaveEvent,
MemberJoinedEvent,
MemberLeftEvent
)
當你的官方帳號加入群組聊天室或多人聊天室時觸發
{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"join","timestamp":1632490426295,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"replyToken":"9e5f8c4cf7ea41d7853c71030831b8ba","mode":"active"}]}
Join event可得到的資訊
當你的官方帳號離開群組聊天室或多人聊天室時觸發
{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"leave","timestamp":1632490394283,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"mode":"active"}]}
Leave event資料沒有replyToken,其他和Join event一樣
當成員加入群組觸發
{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"memberJoined","joined":{"members":[{"type":"user","userId":"Ufd738a53faa5ffea5de965c8e0d5a913"}]},"timestamp":1632489956072,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"replyToken":"01ff21658b634dd390098fd44185d3b4","mode":"active"}]}
當成員離開群組觸發
{"destination":"U91f0036fffbdfc3ead599a54d4883026","events":[{"type":"memberLeft","left":{"members":[{"type":"user","userId":"Ufd738a53faa5ffea5de965c8e0d5a913"}]},"timestamp":1632489890949,"source":{"type":"group","groupId":"C1f357266e227528c3a247b992c6418e7"},"mode":"active"}]}
Leave event資料沒有replyToken,Member join event成員資訊是joined,Member leave event是left,其他和Join event一樣
if(event.message.text=="Get group summary"):
summary=line_bot_api.get_group_summary(event.source.group_id)
message_text="Group summary\n"
message_text+="group id"+summary.group_id+"\n"
message_text+="group name"+summary.group_name+"\n"
message_text+="picture url"+summary.picture_url
message=TextSendMessage(message_text)
if(event.message.text=="Get group members count"):
message=TextSendMessage(line_bot_api.get_group_members_count(event.source.group_id))
這個功能只有驗證過的官方帳號或premium帳號可以使用
member_ids_res = line_bot_api.get_group_member_ids(group_id)
print(member_ids_res.member_ids)
print(member_ids_res.next)
profile = line_bot_api.get_room_member_profile(room_id, user_id)
print(profile.display_name)
print(profile.user_id)
print(profile.picture_url)
if(event.message.text=="Leave group"):
message=TextSendMessage(line_bot_api.leave_group(event.source.group_id))
參考:
https://developers.line.biz/en/reference/messaging-api#Group
https://developers.line.biz/en/reference/messaging-api/#webhook-event-objects
https://github.com/line/line-bot-sdk-python